package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.GsonBean.MetroInfoBean;
import com.lenovo.smarttraffic.bean.MetroDialogEventBusBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by Caleb.F
 * on 2019-11-26
 * at 20:54
 */
public class MyDialogRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private static final Integer TRANFOR_SPOT = 0;
    private static final Integer COMMON_SPOT = 1;
    private static final Integer LAST_SPOT = 2;
    private MetroDialogEventBusBean mMetroDialogEventBusBean;
    private List<MetroInfoBean.ROWSDETAILBean> metroInfoList;
    private HashMap<String, Integer> transforPositionMap;
    private static final String TAG = "MyDialogRecyclerAdapter";
    private HashMap<String, List<String>> transforInfoMap;
    private  RecyclerItemListener mrecyclerItemListener;


    public MyDialogRecyclerAdapter(Context context, MetroDialogEventBusBean metroDialogEventBusBean) {
        mContext = context;
        mMetroDialogEventBusBean = metroDialogEventBusBean;
        metroInfoList = mMetroDialogEventBusBean.getMetroInfoBean().getROWS_DETAIL();
        transforInfoMap = mMetroDialogEventBusBean.getTransforMap();
        Log.d(TAG, "MyDialogRecyclerAdapter: "+transforInfoMap);
        transforPositionMap = new HashMap<>();
        handlerData();
    }

    private void handlerData() {
        for (String item :
                metroInfoList.get(0).getTransfersites()) {
            String s = item.replaceAll("\r\n", "");
            for (int i = 0; i < metroInfoList.get(0).getSites().size(); i++) {
                if (s.equals(metroInfoList.get(0).getSites().get(i))) {
                    transforPositionMap.put(s, i);
                }
            }
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == LAST_SPOT) {
            return new RecyclcerDialogItemViewHolder1(LayoutInflater.from(mContext).inflate(R.layout.recyclerview_last_spot, parent, false));
        } else if (viewType == TRANFOR_SPOT) {
            return new RecyclcerDialogItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recyclerview_transfor_spot, parent, false));

        } else {
            return new RecyclerDialogItemViewHolder2(LayoutInflater.from(mContext).inflate(R.layout.recycler_commom_spot, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
     if(holder instanceof RecyclcerDialogItemViewHolder1){
             ((RecyclcerDialogItemViewHolder1) holder).mTvSpot.setText(metroInfoList.get(0).getSites().get(position));
             ((RecyclcerDialogItemViewHolder1) holder).mTvSpot.setOnClickListener(v->{
                 mrecyclerItemListener.ItemClickedListener(((RecyclcerDialogItemViewHolder1) holder).mTvSpot.getText().toString());
             });
     }
     else if(holder instanceof RecyclerDialogItemViewHolder2){
         ((RecyclerDialogItemViewHolder2) holder).mTvCommonSpot.setText(metroInfoList.get(0).getSites().get(position));
         ((RecyclerDialogItemViewHolder2) holder).mTvCommonSpot.setOnClickListener(v->{
             mrecyclerItemListener.ItemClickedListener(((RecyclerDialogItemViewHolder2) holder).mTvCommonSpot.getText().toString());
         });
     }
     else if(holder instanceof RecyclcerDialogItemViewHolder){
         ((RecyclcerDialogItemViewHolder) holder).mTextView31.setText(metroInfoList.get(0).getSites().get(position));
         Pattern compile = Pattern.compile("[^a-zA-Z0-9]");
         for (Map.Entry<String, List<String>> item:
                 transforInfoMap.entrySet()) {
             if(((RecyclcerDialogItemViewHolder) holder).mTextView31.getText().equals(item.getKey())){
                 StringBuilder stringBuilder = new StringBuilder("可换乘");
                 for (String item1:
                      item.getValue()) {
                     if(!item1.equals(metroInfoList.get(0).getName())){
                     String trim = compile.matcher(item1).replaceAll("").trim();
                     stringBuilder.append(trim).append("、");
                     }
                 }
                 String substring = stringBuilder.substring(0,stringBuilder.toString().length()-1);
                 ((RecyclcerDialogItemViewHolder) holder).mTvTransfor.setText(substring.concat("号线"));
             }
         }
         ((RecyclcerDialogItemViewHolder) holder).mTextView31.setOnClickListener(v->{

             mrecyclerItemListener.ItemClickedListener(((RecyclcerDialogItemViewHolder) holder).mTextView31.getText().toString());
         });


     }

}

    @Override
    public int getItemViewType(int position) {
        for (Map.Entry<String, Integer> item :
                transforPositionMap.entrySet()) {
            if (position == item.getValue() - 1) {
                return LAST_SPOT;
            }
            if (position == item.getValue()) {
                return TRANFOR_SPOT;
            }
        }
        if (position == metroInfoList.get(0).getSites().size() - 1) {
            return LAST_SPOT;
        }
        return COMMON_SPOT;
    }

    @Override
    public int getItemCount() {
        return metroInfoList.get(0).getSites().size();
    }

    class RecyclcerDialogItemViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView31;
       Button mRecyclerRadioButton;
        TextView mLineLeft;
        TextView mLineRight;
        TextView mTvTransfor;

        public RecyclcerDialogItemViewHolder(View convertView) {
            super(convertView);
            mTextView31 = convertView.findViewById(R.id.tv_spot);
            mRecyclerRadioButton = convertView.findViewById(R.id.recycler_radio_button);
            mLineLeft = convertView.findViewById(R.id.line_left);
            mLineRight = convertView.findViewById(R.id.line_right);
            mTvTransfor = convertView.findViewById(R.id.tv_transfor);

        }

    }
    public interface RecyclerItemListener{
        void ItemClickedListener(String LocationName);
    }
    public void setRecyclerItemListener(RecyclerItemListener recyclerItemListener){
        mrecyclerItemListener=recyclerItemListener;
    }

    class RecyclcerDialogItemViewHolder1 extends RecyclerView.ViewHolder {
        TextView mTvSpot;
      Button mRecyclerRadioButton;

        public RecyclcerDialogItemViewHolder1(View convertView) {
            super(convertView);
            mTvSpot = convertView.findViewById(R.id.tv_spot);
            mRecyclerRadioButton = convertView.findViewById(R.id.recycler_radio_button);
        }

    }

    class RecyclerDialogItemViewHolder2 extends RecyclerView.ViewHolder {
        Button mRadioButton;
        TextView mTvCommonSpot;
        TextView mTvLine;
        public RecyclerDialogItemViewHolder2(View convertView) {
            super(convertView);
            mRadioButton = convertView.findViewById(R.id.radioButton);
            mTvCommonSpot = convertView.findViewById(R.id.tv_common_spot);
            mTvLine = convertView.findViewById(R.id.tv_line);
        }
    }
}
